package org.bitterorange.flansmodslain.actions;

import lk.vexview.api.VexViewAPI;
import lk.vexview.gui.components.VexImage;
import lk.vexview.hud.VexImageShow;
import lk.vexview.hud.VexTextShow;
import org.bukkit.entity.Player;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Deprecated
public class ActiveView {
    public static void sendKillTitle(Player player,
                                     String enemyName,
                                     String weaponName,
                                     double distance,
                                     double money,
                                     int points,
                                     boolean isDistantKill,
                                     boolean isMeleeWeaponKill) {
        int x = (VexViewAPI.getPlayerClientWindowWidth(player) - KillTitle.BackgroundImage.getXs()) / 2;
        int y = 20;

        List<VexTextShow> vexTextShowList = new ArrayList<>();
        vexTextShowList.add(new VexTextShow("KillTitleEnemyNameText",
                x + 50,
                y + 7,
                100,
                Collections.singletonList("§c" + enemyName),
                KillTitle.SecondsToKeep,
                KillTitle.EnemyNameTextScale));
        vexTextShowList.add(new VexTextShow("KillTitleWeaponText",
                x + 50,
                y + 29,
                100,
                Collections.singletonList(weaponName),
                KillTitle.SecondsToKeep,
                KillTitle.WeaponNameTextScale));
        List<String> richMessage = new ArrayList<>();
        richMessage.add(String.format("§b击杀距离 §a%.2f", distance));
        if (money > 0.0D) {
            if (points > 0) {
                richMessage.add(String.format("§6你总共获得了§c$%.2f§6和§c%d§6点数!", money, points));
            } else {
                richMessage.add(String.format("§6你总共获得了§c$%.2f§6!", money));
            }
        } else {
            if (points > 0) {
                richMessage.add(String.format("§6你总共获得了§c%d§6点数!", points));
            }
        }
        if (isDistantKill) {
            richMessage.add("§5远距离击杀!");
        }
        if (isMeleeWeaponKill) {
            richMessage.add("§5近身武器击杀!");
        }
        vexTextShowList.add(new VexTextShow("KillTitleRichMessageText",
                -1,
                y + 48,
                100,
                richMessage,
                KillTitle.SecondsToKeep,
                KillTitle.RichMessageTextScale));
        vexTextShowList.forEach(vexTextShow -> VexViewAPI.sendHUD(player, vexTextShow));

        List<VexImage> vexImageList = new ArrayList<>();
        if (isMeleeWeaponKill) {
            vexImageList.add(KillTitle.MeleeWeaponKillIconImage);
        } else {
            vexImageList.add(KillTitle.NormalKillIconImage);
        }
        if (isDistantKill) {
            vexImageList.add(KillTitle.DistantKillIconImage);
        }
        new Thread(() ->
        {
            int i = 0;
            int n = vexImageList.size();
            VexImageShow lastImageShow = null;
            while (i * KillTitle.SecondsPerIcon < KillTitle.SecondsToKeep) {
                if (lastImageShow != null) {
                    VexViewAPI.removeHUD(player, lastImageShow);
                }
                VexViewAPI.sendHUD(player, lastImageShow = new VexImageShow("KillTitleIconImage",
                        vexImageList.get(i % n),
                        KillTitle.SecondsPerIcon));
                // player.sendMessage(String.valueOf(i));
                try {
                    Thread.sleep(KillTitle.SecondsPerIcon * 1000);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
                ++i;
            }
            int p = i * KillTitle.SecondsPerIcon - KillTitle.SecondsToKeep;
            if (p > 0) {
                VexViewAPI.sendHUD(player, new VexImageShow("KillTitleIconImage",
                        vexImageList.get(i % n),
                        KillTitle.SecondsPerIcon));
            }
        }).start();
    }

    public static int getKillTitleKeepSeconds() {
        return KillTitle.SecondsToKeep;
    }

    public static void sendLiteralRankUp(Player player) {
        VexImage rankUpIcon = new VexImage("[local]RankIcons/literal.png",
                -1,
                RankUpBanner.Y,
                RankUpBanner.IconWidth,
                RankUpBanner.IconHeight);
        VexViewAPI.sendHUD(player, new VexImageShow("LiteralRankUpImage", rankUpIcon, 2));
    }

    public static void sendRankUpBanner(Player player, String rankIconName) {
        VexImage rankUpIcon = new VexImage(String.format("[local]RankIcons/%s.png", rankIconName),
                -1,
                RankUpBanner.Y,
                RankUpBanner.IconWidth,
                RankUpBanner.IconHeight);
        VexViewAPI.sendHUD(player, new VexImageShow("RankIcon" + rankIconName, rankUpIcon, 5));
    }

    private static class KillTitle {
        private static final int TopY = 20;
        private static final VexImage BackgroundImage = new VexImage("[local]KillTitle/background.png", -1, TopY, 163, 42);
        private static final VexImage MeleeWeaponKillIconImage = new VexImage("[local]KillTitle/meleeweaponkill.png", -1, TopY, 163, 42);
        private static final VexImage NormalKillIconImage = new VexImage("[local]KillTitle/normalkill.png", -1, TopY, 163, 42);
        private static final VexImage DistantKillIconImage = new VexImage("[local]KillTitle/distantkill.png", -1, TopY, 163, 42);

        // private static final int EnemyNameTextX = -1, EnemyNameTextY = 32;
        private static final double EnemyNameTextScale = 1.8D;

        // private static final int WeaponTextX = -1, WeaponTextY = 54;
        private static final double WeaponNameTextScale = 1.2D;

        // private static final int RichMessageTextX = -1, RichMessageTextY = 68;
        private static final double RichMessageTextScale = 1.0D;

        private static final int SecondsToKeep = 4;
        private static final int SecondsPerIcon = 1;
    }

    private static class RankUpBanner {
        private static final int Y = 25;
        private static final int IconWidth = 96;
        private static final int IconHeight = 96;
    }
}